using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using UCS.XNA.Draughts.Boards;
using UCS.XNA.Draughts.Pieces;
using UCS.XNA.Utils;
using UCS.XNA.Draughts.AI;
using UCS.ArtificialInteligence.MiniMax;
using System.Threading;
using UCS.XNA.Draughts.GameStates;
using UCS.XNA.Draughts.People;

namespace UCS.XNA.Draughts
{
    public class Draughts : Microsoft.Xna.Framework.Game
    {
        #region Private Members

        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        private IGameState _currentState;

        #endregion

        #region Constructors

        public Draughts()
        {
            this._graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.IsMouseVisible = true;
        }

        #endregion

        #region Initialize

        protected override void Initialize()
        {
            base.Initialize();

            GameResources.SpriteBatch = _spriteBatch;
            GameResources.Graphics = _graphics;
            GameResources.Content = Content;

            this.InitializePlayers();
            this.InitializeGameState();
        }

        private void InitializePlayers()
        {
            Players.WhitePlayer = new Player(PieceColor.White, false);
            Players.BlackPlayer = new Player(PieceColor.Black, false);
        }

        private void InitializeGameState()
        {
            InGameState inGameState = new InGameState(new Rectangle(0, 0, _graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight));
            inGameState.Ended += (sender, e) =>
            {
                _currentState = new EndGameState(inGameState.Board, inGameState.Winner);
            };

            _currentState = inGameState;
        }

        #endregion

        #region Load / Unload Content

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
        }

        #endregion

        #region Update

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            this._currentState.Update(gameTime);

            base.Update(gameTime);
        }

        #endregion

        #region Draw

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.LightBlue);

            this._currentState.Draw(gameTime);

            base.Draw(gameTime);
        }

        #endregion
    }
}
